package omg.ligong.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import omg.ligong.server.AdminInfo;
import omg.ligong.server.AdminMgr;
import omg.ligong.util.StrFunc;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action {
	private final static String LOGIN_TYPE_STUDENT = "student";// ѧ��
	private final static String LOGIN_TYPE_TEACHER = "teacher";// ��ʦ
	private final static String LOGIN_TYPE_ADMIN = "admin";// ����Ա

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String type = request.getParameter("type");
		if (LOGIN_TYPE_STUDENT.equals(type)) {
			return studentLogin(mapping, request, response);
		} else if (LOGIN_TYPE_TEACHER.equals(type)) {
			return teacherLogin(mapping, request, response);
		} else if (LOGIN_TYPE_ADMIN.equals(type)) {
			return adminLogin(mapping, request, response);
		}
		return null;
	}

	/**
	 * ѧ���½
	 * 
	 * @param mapping
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	private ActionForward studentLogin(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userid = request.getParameter("userid");
		String password = request.getParameter("password");
		return null;
	}

	/**
	 * ��ʦ��½
	 * 
	 * @param mapping
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	private ActionForward teacherLogin(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userid = request.getParameter("userid");
		String password = request.getParameter("password");
		return null;
	}

	/**
	 * ����Ա��½
	 * 
	 * @param mapping
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	private ActionForward adminLogin(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userid = request.getParameter("userid");
		String password = request.getParameter("password");
		AdminInfo[] admins = AdminMgr.getInstance().getAdmins();
		for (int i = 0; i < admins.length; i++) {
			if (admins[i].getUserid().equals(userid)
					&& admins[i].getPassword().equals(StrFunc.toMd5(password))) {
				HttpSession session = request.getSession();
				session.setAttribute("userid", userid);
				session.setAttribute("username", admins[i].getUsername());
				session.setAttribute("logintype", "admin");
				response.setContentType("text/html;charset=utf-8");
				PrintWriter result = response.getWriter();
				try {
					result.print(admins[i].getUsername());
					result.flush();
				} finally {
					result.close();
				}
				return null;
			}
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter result = response.getWriter();
		try {
			result.print("FAIL");
			result.flush();
		} finally {
			result.close();
		}
		return null;
	}
}
